package com.example.helloworld3;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ooo1---->";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

        Log.d(TAG, "onCreate: "+getTaskId());

        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "你点击了我button1", Toast.LENGTH_LONG).show();
                /*try {
                    Thread.sleep(5000);
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
                finish();*/
                // 显示 Intent
                /*Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);*/

                // 隐式 Intent
               /* String data = "你好我是Intent 的数据！";
                Intent intent = new Intent("com.example.helloworld3.ACTION_START");
                intent.addCategory("com.example.helloworld3.MY_CATEGORY");
                intent.putExtra("extra_data",data);
                startActivity(intent);*/

                //
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                // 返回数据给上一个活动
//                startActivityForResult(intent,1);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    String extra = data.getStringExtra("extra_data");
                    Log.d("ooo--->","上个activity返回的数据是："+extra);
                }
                break;
            default:
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_item:
                Toast.makeText(this, "You clicked Add", Toast.LENGTH_LONG).show();
                break;
            case R.id.remove_item:
//                Toast.makeText(this, "You clicked Remove", Toast.LENGTH_LONG).show();
                /**
                 *  打开微信
                 */
                Intent intent = new Intent();
                ComponentName cmp=new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(cmp);
                startActivity(intent);
                break;
            default:
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
